package com.sakuraio.nextj.core.protocol;

import cn.hutool.core.convert.Convert;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * <p>Request</p>
 *
 * @author nekoimi 2023/03/14
 */
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "通用请求体")
public class Request<T> implements Serializable {
    @Schema(description = "分页参数")
    private Page page = Page.DEFAULT_PAGE;

    @Schema(description = "请求参数")
    private T params;

    /**
     * 获取分页参数
     *
     * @return
     */
    public Page page() {
        return page;
    }

    /**
     * 获取请求参数
     *
     * @return
     */
    public T params() {
        return this.params;
    }

    /**
     * 参数类型快捷转换
     *
     * @param paramsType 参数转换类型
     * @return P
     */
    public <P> P convert(Class<P> paramsType) {
        return Convert.convert(paramsType, this.params);
    }

    /**
     * 获取字符串参数请求
     *
     * @param requestValue 字符串内容
     * @return
     */
    public static Request<String> stringRequest(String requestValue) {
        return new Request<>(Page.DEFAULT_PAGE, requestValue);
    }

    /**
     * 获取布尔值请求
     *
     * @param requestValue 布尔值内容
     * @return
     */
    public static Request<Boolean> booleanRequest(Boolean requestValue) {
        return new Request<>(Page.DEFAULT_PAGE, requestValue);
    }
}
